<?php
/**
 * Object Relational Persistence Mapping Class for table: default_usage
 *
 * @package	com.athena
 * @author	LHV
 */
class DefaultUsage extends ORDataObject {

	/**#@+
	 * Fields of table: default_usage mapped to class members
	 */
	var $default_usage_id		= '';
	var $usage_title_enum		= '';
	var $usage_num		= '';
	var $dosage		= '';
	var $dosage_title_enum		= '';
	var $time		= '';
	var $quantity_num		= '';
	var $quantity_title_enum		= '';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'default_usage';

	/**
	 * Primary Key
	 */
	var $_key = 'default_usage_id';
	
	/**
	 * Internal Name
	 */
	var $_internalName = 'DefaultUsage';

	/**
	 * Handle instantiation
	 */
	function DefaultUsage() {
		parent::ORDataObject();
	}

    /**
     * Check whether any default usage match on couplt (usage, dosage).
     *
     * Usage and Dosage make an default usage unique
     *
     * @param int $usageTitleEnum
     * @param int $dosageTitleEnum
     * @return default_usage_id if exist or -1 if not
     */
    function checkExist($usageTitleEnum, $dosageTitleEnum) {
        $ds = new Datasource_sql();
        $sql = array (
            'cols'    => "$this->_key",
            'from'    => "$this->_table",
            'where'   => "usage_title_enum = $usageTitleEnum AND dosage_title_enum = $dosageTitleEnum",
            'limit'   => "1"
        );
        
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        if (empty($result)) {
            return -1;
        }
        return $result[0][$this->_key];
    }
    
    function listAll() {
        $ds = new Datasource_sql();
            
        $sql = array (
            'cols'      => "*",
            'from'      => "$this->_table",
            'orderby'   => "usage_title_enum DESC"
        );
        $ds->setup($this->_db, $sql);
        return $ds->toArray();
    }
}
?>
